package com.zzy.customglide.manager;

import android.content.Context;
import android.util.Log;
import android.widget.ImageView;

import com.zzy.customglide.cache.ActiveCache;
import com.zzy.customglide.cache.MemoryCache;
import com.zzy.customglide.cache.MemoryCacheCallBack;
import com.zzy.customglide.cache.disk.DiskCacheImpl;
import com.zzy.customglide.load.LoadDataManager;
import com.zzy.customglide.load.ResponseListener;
import com.zzy.customglide.resource.Key;
import com.zzy.customglide.resource.Value;
import com.zzy.customglide.resource.ValueCallback;

public class LoadEngine implements LifecycleCallBack, ValueCallback, MemoryCacheCallBack, ResponseListener {
    private static final String TAG = "LoadEngine";
    private final int MEMORY_MAX_SIZE = 1024 * 1024 * 60;

    ActiveCache activeCache;
    MemoryCache memoryCache;
    DiskCacheImpl diskCache;
    private String urlPath;
    private Context context;
    private String key;
    private ImageView imageView;

    @Override
    public void glideStartAction() {
        Log.i(TAG, "glideStartAction");
    }

    @Override
    public void glideStopAction() {
        Log.i(TAG, "glideStopAction");
    }

    @Override
    public void glideReleaseAction() {
        Log.i(TAG, "glideReleaseAction");
        if(activeCache!=null){
            activeCache=null;
        }
    }

    public LoadEngine() {
        if (activeCache == null) {
            activeCache = new ActiveCache(this);
        }
        if (memoryCache == null) {
            memoryCache = new MemoryCache(MEMORY_MAX_SIZE);
            memoryCache.setMemoryCacheCallBack(this);
        }
        if (diskCache == null) {
            diskCache = new DiskCacheImpl();
        }
    }


    //  RequestManager传递的值
    public void loadValue(String url, Context context) {
        this.urlPath = url;
        this.context = context;
        key = new Key(url).getKey();
    }

    public void into(ImageView imageView) {
        this.imageView = imageView;

        Value value = loadAction();
        if (null != value) {
            imageView.setImageBitmap(value.getmBitmap());
            value.nonUseAction();
        }
       ;

    }

    private Value loadAction() {
        Value value = activeCache.get(key);
        if (value != null) {
            //加载成功
            value.useAction();
            return value;
        }
        //从内存缓存中查找。如果找到，插入到活动缓存中
        value = memoryCache.get(key);
        if (value != null) {
            memoryCache.handRemove(key);
            activeCache.put(key, value);
            value.useAction();
            return value;
        }
        //磁盘缓存中
        value = diskCache.get(key);
        if (value != null&&value.getmBitmap()!=null) {
            activeCache.put(key, value);
            value.useAction();
            return value;
        }

        //加载网络图片啦
        value = new LoadDataManager().loadResource(urlPath, this, context);
        if (value != null) {
            return value;
        }
        return null;
    }

    //活动缓存移除监听
    @Override
    public void valueNonUseListener(String key, Value value) {
        if(key!=null&&value!=null){
            memoryCache.put(key,value);
        }
    }

    //内存缓存移除监听
    @Override
    public void entryRemoveMemory(String key, Value value) {

    }

    //
    @Override
    public void responseSuccess(Value value) {
        if (null != value) {
            imageView.setImageBitmap(value.getmBitmap());
        }
        value.setKey(key);
        if (diskCache != null) {
            diskCache.put(key, value);
        }

    }

    @Override
    public void responseException(Exception exception) {
        Log.e("LoadEngine", "加载异常：" + exception.getMessage());
    }
}
